package hu.cskassai.sad.business.helper;

import hu.cskassai.sad.model.factory.DomainFactory;
import hu.cskassai.sad.model.factory.hibernate.DomainFactoryImpl;
import hu.cskassai.sad.model.graph.Edge;
import hu.cskassai.sad.model.graph.Vertex2D;

import java.util.HashMap;
import java.util.Map;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;

public class GraphGenerator {
	
	private static DomainFactory domainFactory = new DomainFactoryImpl();
	
	public static Graph<Vertex2D, Edge> generateWeb(final int startPointX, final int startPointY,
		final int horizontalPiece, final int horizontalGap, final int verticalPiece, final int verticalGap)
	{
		final Graph<Vertex2D, Edge> graph = new UndirectedSparseGraph<Vertex2D, Edge>();
		Map<Integer, Vertex2D> upperVertexMap = new HashMap<Integer, Vertex2D>();
		for (int i = 0; i < verticalPiece; i++) {
			Vertex2D previousVertex = null;
			for (int j = 0; j < horizontalPiece; j++) {
				Vertex2D vertex2d = domainFactory.createVertex2d(i * horizontalGap + startPointX, j * verticalGap + startPointY);
				graph.addVertex(vertex2d);
				if(previousVertex != null){
					Edge edge = domainFactory.createEdge(previousVertex, vertex2d);
					graph.addEdge(edge, previousVertex, vertex2d);
				}
				Vertex2D upperVertex = upperVertexMap.get(j);
				if(upperVertex!= null){
					Edge edge = domainFactory.createEdge(upperVertex, vertex2d);
					graph.addEdge(edge, upperVertex, vertex2d);
				}
				previousVertex = vertex2d;
				upperVertexMap.put(j, vertex2d);
			}
		}
		return graph;
	}
	
}
